package testng.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.FlowEngineApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@SpringBootTest(classes = FlowEngineApplication.class)
public class FlowNoticeTemplateControllerTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @BeforeClass
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    private Long id=91L;

    //@Test
    public void  listTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/notice/template/list").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"searchText\":\"www\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void addTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/notice/template/add").characterEncoding("UTF-8")
                .content("{\"name\": \"新增模板测试\", \"type\": \"REJECT\", \"title\": \"节点名称\",\n" +
                        " \"content\": \"新增模板测试新增模板测试新增模板测试\",\"status\":true}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        id=Long.valueOf(String.valueOf(jsonObject.get("datas")));
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }
    //@Test
    public void editTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/notice/template/edit").characterEncoding("UTF-8")
                .content("{\"name\": \"新增模板测试\", \"type\": \"REJECT\", \"title\": \"节点名称\",\n" +
                        " \"content\": \"新增模板测试新增模板测试新增模板测试\",\"status\":false,\"id\":"+id+"}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void detailTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/notice/template/detail").characterEncoding("UTF-8")
                .content("{\"id\":"+id+"}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }
    //@Test
    public void startStatusTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/notice/template/startStatus").characterEncoding("UTF-8")
                .content("{\"id\":"+id+",\"type\":\"REJECT\",\"status\":false}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }
    //@Test
    public void listNoticeTemplateParameterTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .get("/notice/template/listNoticeTemplateParameter").characterEncoding("UTF-8")
                .contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }
    //@Test
    public void listNoticeTemplateTypeTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .get("/notice/template/listNoticeTemplateType").characterEncoding("UTF-8")
                .contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }
    //@Test
    public void sendNoticeMessageToRedisTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .get("/notice/template/sendNoticeMessageToRedis?procId=c5ad9780a76e11e99feb207918fcc7cd").characterEncoding("UTF-8")
                .contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void deleteTest() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/notice/template/delete").characterEncoding("UTF-8")
                .content("{\"id\":"+id+"}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

}
